import Vue from "vue";
import VueRouter from "vue-router";

import routes from "./routes";

Vue.use(VueRouter);

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes
});

/**
 * 需要用户登录权限的页面列表
 */
const list = ["OrderList", "Coupon", "Wallet", "OrderConfirm"];

router.beforeEach((to, from, next) => {
  if (list.includes(to.name)) {
    // 访问的页面name包含list,需要权限认证
    if (sessionStorage.isLogin === "true") {
      // 用户已经登陆,直接next()访问
      next();
    } else {
      // 否则携带真实要跳转到的页面路径,以便登陆后继续向真实页面跳转
      next({
        name: "Login",
        params: { to }
      });
    }
  } else {
    // 不需要权限 页面
    next();
  }
});
export default router;
